package cn.edu.zafu.bigdata.x.huangxihao202211080211.TCP1;

import java.io.*;
import java.net.*;

public class Server11 {
    public static void main(String[] args) {
        // 设置服务端端口号
        int port = 12345;

        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("服务器已启动，等待客户端连接...");

            // 等待客户端连接
            Socket clientSocket = serverSocket.accept();
            System.out.println("客户端已连接，IP: " + clientSocket.getInetAddress());

            // 获取输入和输出流
            BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter output = new PrintWriter(clientSocket.getOutputStream(), true);

            String message;
            while ((message = input.readLine()) != null) {
                System.out.println("客户端: " + message);
                // 回复客户端消息
                output.println("服务器: " + message);
            }

            // 关闭连接
            input.close();
            output.close();
            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
